package com.smzd.framework.system.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.smzd.core.dao.AbstractDao;
import com.smzd.core.domain.Many2Many;
import com.smzd.core.mappers.IMapper;
import com.smzd.framework.system.domain.Group;
import com.smzd.framework.system.domain.Meeting;
import com.smzd.framework.system.mappers.GroupMapper;
import com.smzd.framework.utils.db.ORPage;

@Component
public class GroupDao extends AbstractDao<Group> {
    @Autowired
    private GroupMapper mapper;

    @Override
    protected IMapper<Group> getMapper() {
        return mapper;
    }

    public ORPage<Meeting, Group> getUserGroupListByPage(ORPage<Meeting, Group> page) {
        preparePage(page);
        List<Group> elems = mapper.getUserGroupListByPage(page);
        page.setElems(elems);
        int total = mapper.getElemsCount(page);
        page.setTotalCount(total);
        completePage(page);
        return page;
    }

    public void addUserToGroups(int userId, Integer... groupIds) {
        for (int groupId : groupIds) {
            mapper.insertUserGroup(new Many2Many(userId, groupId));
        }
    }

    public void removeUserFromGroups(int userId, Integer... groupIds) {
        for (int groupId : groupIds) {
            mapper.removeUserGroup(new Many2Many(userId, groupId));
        }
    }

    public List<Group> getGroupsByUserId(int uid) {
        return mapper.getGroupsByUserId(uid);
    }
}
